package evtop

import (
	"github.com/pkg/errors"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/evtop/collecting"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/groupd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/infod"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/log"
)

func newCollectingListener(d ddd.Domain) *collectingListener {
	return &collectingListener{
		base: base{
			d: d,
		},
		iar:  d.SubDomain(infod.DomainID).AggregateRootByID(infod.DomainID).(infod.InfoAggregateRoot),
		coll: collecting.NewCollecting(d),
	}
}

type collectingListener struct {
	base
	coll collecting.Collecting
	iar  infod.InfoAggregateRoot
}

func (l collectingListener) StartOk() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {
		log.Debugf("执行事件监听:collectingListener StartOk :%#v", evt)
		l.coll.Start()
		return nil
	})
}
func (l collectingListener) onInfoAdd() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {
		log.Debugf("执行事件监听:collectingListener onInfoAdd :%#v", evt)
		id, ok := evt.Data.(string)
		if !ok {
			return errors.Errorf("获取事件数据格式错误：%#v", evt.Data)
		}
		info, err := l.iar.GetInfo(ctx, id)
		if err != nil {
			log.Warn("collectingListener onInfoAdd 获取信息失败：%+v", err)
			return nil
		}
		if err != nil {
			return errors.Wrapf(err, "获取信息失败：%v", err)
		}

		if info.Status == infod.IStatusPublished {
			l.coll.Append(info)
		}
		return nil
	})

}
func (l collectingListener) onInfoPublish() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {
		log.Debugf("执行事件监听:collectingListener onInfoPublish :%#v", evt)
		eadata, ok := evt.Data.(*infod.EvtActData)
		if !ok {
			return errors.Errorf("获取事件数据格式错误：%#v", evt.Data)

		}
		l.coll.Append(eadata.New)
		// 触发组内信息的添加
		return nil
	})
}
func (l collectingListener) onInfoUnpublish() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {

		log.Debugf("执行事件监听:collectingListener onInfoUnpublish :%#v", evt)
		eadata, ok := evt.Data.(*infod.EvtActData)
		if !ok {
			return errors.Errorf("获取事件数据格式错误：%v", evt.Data)
		}
		l.coll.Del(eadata.New.InfoID)
		return nil
	})
}

// onInfoDelete 信息删除时，修改相应的栏目统计信息
func (l collectingListener) onInfoDelete() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {

		log.Debugf("执行事件监听: collectingListener onInfoDelete :%#v", evt)
		eadata, ok := evt.Data.(*infod.EvtActData)
		if !ok {
			return errors.Errorf("获取事件数据格式错误：%#v", evt.Data)
		}
		// 不是互动平台 并且删除前不是已删除信息
		if eadata.Old.Status == infod.IStatusPublished {
			l.coll.Del(eadata.Old.InfoID)
		}
		return nil
	})
}
func (l collectingListener) onStartCollecting() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {

		log.Debugf("执行事件监听: collectingListener onStartCollecting :%#v", evt)
		group, ok := evt.Data.(*groupd.GroupAggregate)
		if !ok {
			return errors.Errorf("获取事件数据格式错误：%#v", evt.Data)
		}
		if group.Kind == groupd.GroupKindAuto {
			l.coll.AddGroup(group)
		}
		return nil
	})
}
func (l collectingListener) onStopCollecting() ddd.EventListener {
	return l.creEventListener(func(ctx ddd.Context, evt ddd.Event) error {

		log.Debugf("执行事件监听: collectingListener onStartCollecting :%#v", evt)
		group, ok := evt.Data.(*groupd.GroupAggregate)
		if !ok {
			return errors.Errorf("获取事件数据格式错误：%#v", evt.Data)
		}
		if group.Kind == groupd.GroupKindAuto {
			l.coll.DelGroup(group)
		}
		return nil
	})
}
